var _that;
var app = getApp();
var goodsPerHgt = 80;
var scaleAmt = wx.createAnimation({
  duration: 200,
  timingFunction: 'ease-out'
});

function carInit(target) {
  _that = target;
  _that.setData({
    needshow: false,
    carscrollhgt: 0
  });

  _that.animation = wx.createAnimation({
    duration: 300,
    timingFunction: 'ease-out',
    // 延迟多长时间开始
    delay: 0,
    transformOrigin: 'left bottom 0',
    success: function (res) {
      console.log(res)
    }
  });

  loadCarData();
}

function showDetail() {
  // console.log('showcar');
  if (_that.data.totalcount <= 0) {
    return;
  }
  var topoffset = _that.data.carscrollhgt;
  _that.setData({
    needshow: true,
    carTopOffset: topoffset
  });


  _that.animation.top(0).step()

  _that.setData({
    car_amt: _that.animation.export()
  });
}

function closeDetail() {
  _that.setData({
    needshow: false
  });
}

function update() {

}

function submitOrder(orderinfo) {
  if (_that.data.address == null || _that.data.address == undefined) {
    wx.showToast({
      title: '请先添加收货地址！',
      icon: 'none'
    });
    return;
  }
  if (_that.data.totalcount <= 0) {
    return;
  }
  var orderinfo = {
    mid: _that.data.mid,
    fid: _that.data.fid,
    stationId: _that.data.stationId,
    goodslist: _that.data.goodslist
  }
  wx.navigateTo({
    url: '../neworder/neworder?type=cake&orderinfo=' + encodeURIComponent(JSON.stringify(orderinfo)) + '&address=' + encodeURIComponent(JSON.stringify(_that.data.address)),
  });
}

function startAnimation() {
  scaleAmt.scale(1.5, 1.5).step();
  _that.setData({
    scaleAmt: scaleAmt.export()
  });
  var timeid = setTimeout(function () {
    clearTimeout(timeid);
    scaleAmt.scale(1, 1).step();
    _that.setData({
      scaleAmt: scaleAmt.export()
    });
  }, 300);
}

function handleCount(e) {
  startAnimation();
  var options, goodsid;
  if (e.currentTarget) {
    options = parseInt(e.currentTarget.dataset.options);
    goodsid = e.currentTarget.dataset.goodsid;
  }
  else {
    options = e.options;
    goodsid = e.goodsid;
  }
  var goodslist = _that.data.goodslist;
  var goodsidlist = _that.data.goodsidlist;
  var totalcount = parseInt(_that.data.totalcount);
  var totalprice = parseFloat(_that.data.totalprice);

  if (options == '1') {
    totalcount += 1;
    if (goodsidlist.indexOf(goodsid) >= 0) {
      goodslist[goodsidlist.indexOf(goodsid)].num = parseInt(goodslist[goodsidlist.indexOf(goodsid)].num) + 1;
      totalprice += parseFloat(goodslist[goodsidlist.indexOf(goodsid)].price);
    }
    else {
      totalprice += parseFloat(e.price);
      goodsidlist.push(goodsid);
      var tmpinfo = {
        goodsid: goodsid,
        ID: goodsid,
        num: '1',
        name: e.name,
        shortname: e.shortname,
        price: e.price,
        img: e.img,
        image: e.image,
        icon: e.icon,
        sku: e.sku,
        spec: e.spec,
        size: e.size,
        tips: e.tips
      };
      goodslist.push(tmpinfo);
    }
  }
  else {
    if (totalcount > 0 && goodsidlist.indexOf(goodsid) >= 0) {
      totalcount -= 1;
      totalprice -= parseFloat(goodslist[goodsidlist.indexOf(goodsid)].price);
      goodslist[goodsidlist.indexOf(goodsid)].num = parseInt(goodslist[goodsidlist.indexOf(goodsid)].num) - 1;
      if (parseInt(goodslist[goodsidlist.indexOf(goodsid)].num) <= 0) {
        goodslist.splice(goodsidlist.indexOf(goodsid), 1);
        goodsidlist.splice(goodsidlist.indexOf(goodsid), 1);
      }
    }
  }

  console.log(goodsidlist);
  totalprice = app.keepTwoDecimalFull(totalprice);
  _that.setData({
    totalcount: totalcount,
    totalprice: totalprice,
    goodslist: goodslist,
    goodsidlist: goodsidlist
  });

  adjustHeight();
  if (totalcount == 0) {
    _that.setData({
      needshow: false
    });
  }
}

module.exports = {
  init: carInit,
  showCar: showDetail,
  closeCar: closeDetail,
  update: update,
  submitOrder: submitOrder,
  handleCount: handleCount
}

//------以下方法不外调
function loadCarData() {
  var totalcount = 0;
  var totalprice = 0.00;
  _that.setData({
    totalcount: totalcount,
    totalprice: totalprice,
    goodslist: [],
    goodsidlist: []
  });
  adjustHeight();
}

function adjustHeight() {
  var hgt = _that.data.goodslist.length * goodsPerHgt;
  var win_hgt = wx.getSystemInfoSync().windowHeight;
  if (hgt > win_hgt * 0.5) {
    hgt = win_hgt * 0.5;
  }
  hgt += 70;
  _that.setData({
    carscrollhgt: hgt,
    goodsPerHgt: goodsPerHgt
  });
}
